package code.structure.manager.others
{
	import code.main.AppData;
	import code.structure.manager.sound.MySoundManager;
	import code.untils.GameSettingConfig;
	import code.untils.LogConst;
	import code.untils.SharedObjectConst;
	
	import flash.net.SharedObject;
	import flash.utils.Dictionary;
	
	/**
	 * 
	 * @author Yuri
	 * 
	 */
	public class SharedObjectManager
	{
		private var _sharedObject:SharedObject;
		private static var _instance:SharedObjectManager;
		public function SharedObjectManager()
		{
			if(_instance) throw new Error("实例化单例类出错-GlobalManager");
			getSharedObjectByKey();
		}
		
		public static function getIntance():SharedObjectManager{
			if(!_instance){
				_instance = new SharedObjectManager();
			}
			return _instance;
		}
		
		/**
		 *获取一个sharedOnject通过索引 
		 * @param key
		 * @return 
		 * 
		 */
		public function getSharedObjectByKey(key:String=SharedObjectConst.DEFAULT_SHARED_NAME):SharedObject{
			if(_sharedObject) return _sharedObject;
			key = key+"_"+AppData.myId;
			try{
				_sharedObject = SharedObject.getLocal(key,"/");
				check();
				}catch(error:Error){
					StatisticsManager.writeLog(LogConst.ERROR_INITDATA_ANALY, 9, error.toString().slice(0,500));
					_sharedObject = null;
				}	
			return _sharedObject;
		}
		
		/**
		 *设置值 
		 * @param subKey
		 * @param subData
		 * 
		 */
		public function setSharedObjectData(subKey:String=null,subData:* = null):void{
			_sharedObject = getSharedObjectByKey();
			if(!_sharedObject) return;
			if(subKey){
				_sharedObject.data[subKey] = subData;
				_sharedObject.flush();
			}
		}
		
		/**
		 *分析本地有的存取信息是否会改变游戏设置 
		 * 
		 */
		public function analyerSharedGameInfo():void{
			_sharedObject = getSharedObjectByKey();
			if(!_sharedObject) return;
			if(!_sharedObject) {
				MySoundManager.bMuise = true;
				MySoundManager.bSound = true;
				return;
			}
			if(_sharedObject.data.hasOwnProperty(SharedObjectConst.SETTING_MUSIC)){
				MySoundManager.bMuise = Boolean(_sharedObject.data[SharedObjectConst.SETTING_MUSIC]);
			}else{
				MySoundManager.bMuise = true;
			}
			if(_sharedObject.data.hasOwnProperty(SharedObjectConst.SETTING_SONG_EFFECT)){
				MySoundManager.bSound = Boolean(_sharedObject.data[SharedObjectConst.SETTING_SONG_EFFECT]);
			}else{
				MySoundManager.bSound = true;
			}
		}
		
		/**
		 *默认存取数据  本地存取管理器
		 */
		public function get sharedObject():SharedObject
		{
			return _sharedObject;
		}
		/**
		 *获取默认存取数据中的具体key值
		 */
		public function getSharedObjectKeyData(subKey:String):*
		{
			_sharedObject = getSharedObjectByKey();
			if(!_sharedObject) return null;
			if(_sharedObject.data.hasOwnProperty(subKey)){
				return  _sharedObject.data[subKey];
			}
			return null;
		}
		
		/**
		 * 清除缓存 
		 * 
		 */
		public function clearSharedObject():void{
			if(_sharedObject){
				_sharedObject.clear();
				_sharedObject = null;
			}
				
		}
		
		/**
		 * 验证是否需要转换
		 * 
		 */
		private function check():void{
			var version:String = getSharedObjectKeyData(SharedObjectConst.VERSION);
			if(version && version == SharedObjectConst.CURRENT_VERSION)return;
			convertShareObject();
		}
		/**
		 *切换成最新的 
		 * 
		 */
		private function convertShareObject():void{
			//to do记录临时需要的缓存
			var music:Object;
			var voice:Object;
			music = getSharedObjectKeyData(SharedObjectConst.SETTING_MUSIC);
			voice = getSharedObjectKeyData(SharedObjectConst.SETTING_SONG_EFFECT);
			//to do 删除当前的缓存部分信息
			clearSharedObject();
			
			_sharedObject = SharedObject.getLocal(SharedObjectConst.DEFAULT_SHARED_NAME+"_"+AppData.myId,"/");
			//to do 部分缓存转移
			if(music) setSharedObjectData(SharedObjectConst.SETTING_MUSIC,music);
			if(voice) setSharedObjectData(SharedObjectConst.SETTING_SONG_EFFECT,voice);
			//to do设定版本
			setSharedObjectData(SharedObjectConst.VERSION,SharedObjectConst.CURRENT_VERSION);
		}
		
	}
}